
package central;

import java.io.*;
import java.net.*;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author rfg0807
 */
public class Empfaenger extends Thread {

    public static void main(String args[]) {
        try {
            int port = 8888;
            DatagramSocket dsocket = new DatagramSocket(port);  // Create a socket to listen on the port.
            byte[] buffer = new byte[1024];                     // Create a buffer to read datagrams into. If a packet is larger than this buffer, the excess will simply be discarded!
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);  // Create a packet to receive data into the buffer
            while (true) {                  // Now loop forever, waiting to receive packets and printing them.
                dsocket.receive(packet);    // Wait to receive a datagram
                String msg = new String(buffer, 0, packet.getLength());               // Convert the contents to a string and ...
                System.out.println(packet.getAddress().getHostName() + ": " + msg);   // ... display them
                packet.setLength(buffer.length);    // Reset the length of the packet before reusing it.
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}
    
/*
    
package central;

import java.io.*;
import java.net.*;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;


 //@author SchabauerJ
 
public class Empf extends Thread {

    private Logger logger = null;
   
    //IP Einstellungen
    
    private DatagramSocket clientSocket = null;
    private InetAddress IPAddress = null;
    private int IPPort = 8888;  //default
    private int sendInterval = 1000;  //Send Interval for Telegrams in ms

    // Konstruktor
     
    Empf() {
        logger = Logger.getLogger(central.Empf.class.getName());
        //logger.info("Object 'sender' initialized");
    }

    public void connect(String ip_addr, int port) {
        //BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
        try {
            clientSocket = new DatagramSocket();
            IPAddress = InetAddress.getByName(ip_addr);
            IPPort = port;
            this.start();

        } catch (SocketException ex) {
            Logger.getLogger(UDP_Client.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnknownHostException ex) {
            Logger.getLogger(UDP_Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 

    
      private byte[] receivedTel() {
      try
      {
        long timeMillis = System.currentTimeMillis();
        ByteArrayInputStream bos = new ByteArrayInputStream();
        DataInputStream dos = new DataInputStream(bos);
        dos.readbyte(timeMillis);
        dos.flush();
        currentTel = bos.toByteArray();
     }
     
     catch (IOException ex)
      {
      logger.log(Level.SEVERE, null, ex);
      }
     return receivedTel;
     }
     

    /*
     * User Interface (API)
     */

      
    
/*
     public void setCMD(String _cmd) {
        cmd = _cmd.charAt(0);
    }

    public void setCMD(char _cmd) {
        cmd = _cmd;
    }
*/
      
      
    /*
     * RUN The purpose of this thread is to continuously poll the servoStates
     * and send a telegram to the client
     */
/*    
    @Override
    public void run() {
        logger.info("Thread 'Empf' created and running");
        while (true) {
            try {
                byte[] receivedTel = receivedTel();
               
                 DatagramPacket receivePacket = new
                 DatagramPacket(receiveData, receiveData.length);
                 clientSocket.receive(receivePacket); 
                 String modifiedSentence = new String(receivePacket.getData());
                 System.out.println("FROM SERVER:" + modifiedSentence);
                 
                //Thread.sleep(sendInterval);
            } catch (InterruptedException ex) {
                logger.log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
        }
    }
}
*/